package com.zhang.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mysql.jdbc.StringUtils;
import com.zhang.mapper.IProductMapper;
import com.zhang.pojo.InputCarBean;
import com.zhang.pojo.OutputCarBean;
import com.zhang.pojo.ProductBean;
import com.zhang.service.IProductService;
import com.zhang.service.ITypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 库存业务接口
 */
@Service
@Transactional
public class ProductServiceImpl implements IProductService {
    @Autowired
    private IProductMapper productMapper;
    @Autowired
    private ITypeService typeService;

    @Override
    public IPage<ProductBean> findByItemAndPage(Page<ProductBean> page, String name, Integer typeId, Integer startNum, Integer endNum) {
        QueryWrapper<ProductBean> wrapper = new QueryWrapper<>();
        if (!StringUtils.isNullOrEmpty(name)){
            wrapper.like("p_name",name);
        }if (typeId != null && typeId != 0){
            wrapper.eq("fk_typeId",typeId);
        }if (startNum != null && startNum != 0){
            wrapper.ge("p_count",startNum);
        }if (endNum != null && endNum != 0){
            wrapper.le("p_count",endNum);
        }
        Page<ProductBean> productBeanPage = productMapper.selectPage(page, wrapper);
        List<ProductBean> records = productBeanPage.getRecords();
        List<ProductBean> productBeans = new ArrayList<>();
        for (int i = 0; i < records.size(); i++) {
            ProductBean productBean = records.get(i);
            productBean.setTypeBean(typeService.findById(productBean.getTypeId()));
            productBeans.add(productBean);
        }
        Page<ProductBean> productBeanPage1 = productBeanPage.setRecords(records);
        return productBeanPage1;
    }

    @Override
    public ProductBean findById(int id) {
        ProductBean productBean = productMapper.selectById(id);
        productBean.setTypeBean(typeService.findById(productBean.getTypeId()));
        return productBean;
    }

    @Override
    public void updateMoney(int id, int money) {
        ProductBean productBean = new ProductBean();
        productBean.setSale(money);
        UpdateWrapper<ProductBean> wrapper = new UpdateWrapper<>();
        wrapper.eq("pk_productId",id);
        productMapper.update(productBean,wrapper);
    }

    @Override
    public void updateNum(int id, int number) {
        ProductBean productBean = new ProductBean();
        productBean.setCount(number);
        UpdateWrapper<ProductBean> wrapper = new UpdateWrapper<>();
        wrapper.eq("pk_productId",id);
        productMapper.update(productBean,wrapper);
    }

    @Override
    public void add(List<InputCarBean> inputCarBean) {//购物车集合


        List<ProductBean> productBeans = productMapper.selectList(null);//获取所有的商品集合

        //遍历购物车集合
        A:for (int i = 0; i < inputCarBean.size(); i++) {
            InputCarBean carBean = inputCarBean.get(i);


            for (int j = 0; j < productBeans.size(); j++) {
                ProductBean productBean = productBeans.get(j);
                if (carBean.getNumber().equals(productBean.getNumber())){
                    int num = carBean.getCount() + productBean.getCount();
                    this.updateNum(productBean.getId(),num);
                    continue A;
                }
            }

            ProductBean productBean1 = new ProductBean();
            productBean1.setNumber(carBean.getNumber());
            productBean1.setName(carBean.getName());
            productBean1.setMill(carBean.getMill());
            productBean1.setPurchase(carBean.getPurchase());
            productBean1.setSale(carBean.getSale());
            productBean1.setCount(carBean.getCount());
            productBean1.setImg(carBean.getImg());
            System.out.println("========================>" + carBean.getTypeId());
            productBean1.setTypeId(carBean.getTypeId());
            productMapper.insert(productBean1);
        }


    }

    @Override
    public void del(int id) {
        productMapper.deleteById(id);
    }

    @Override
    public void output(List<OutputCarBean> outputCarBeans) {
        List<ProductBean> productBeans = productMapper.selectList(null);//获取所有的商品集合

        for (int i = 0; i < outputCarBeans.size(); i++) {
            OutputCarBean outputCarBean = outputCarBeans.get(i);

            for (int j = 0; j < productBeans.size(); j++) {
                ProductBean productBean = productBeans.get(j);

                if (outputCarBean.getNumber().equals(productBean.getNumber())){
                    if (outputCarBean.getCount().equals(productBean.getCount())){
                        del(productBean.getId());
                        break;
                    }

                    UpdateWrapper<ProductBean> wrapper = new UpdateWrapper<>();
                    wrapper.eq("pk_productId",productBean.getId());
                    ProductBean bean = new ProductBean();
                    bean.setCount(productBean.getCount() - outputCarBean.getCount());
                    productMapper.update(bean,wrapper);
                }
            }
        }

    }
}
